<!-- 新增和修改 -->
<template>
  <div class="app-container">
    <Edialog
      :outershow="outershow"
      :title="title"
      @saveFn="saveFn"
      @close="closeFn"
      :saveClick="type == 'edit' ? true : false"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="type == 'edit' ? rules : {}"
        label-width="120px"
        v-loading="formLoading"
      >
        <el-form-item label="函数名称" prop="funcName">
          <div>{{ form.funcName }}</div>
        </el-form-item>

        <el-form-item label="函数标识" prop="funcCode">
          <div>{{ form.funcCode }}</div>
        </el-form-item>

        <el-form-item label="参数说明" prop="paramsRemark">
          <el-input
            v-model="form.paramsRemark"
            type="textarea"
            v-if="type !== 'detail'"
          />
          <div v-else>{{ form.paramsRemark }}</div>
        </el-form-item>
        <el-form-item label="函数说明" prop="remark">
          <el-input
            v-model="form.remark"
            type="textarea"
            v-if="type !== 'detail'"
          />
          <div v-else>{{ form.remark }}</div>
        </el-form-item>
      </el-form>
    </Edialog>
  </div>
</template>

<script>
import { postBsFunctionApi, BsFunctionByIdApi } from "@/api/fxManage";
import Edialog from "@/components/Edialog";

export default {
  data() {
    return {
      formLoading: false,
      type: "",
      outershow: false,
      title: "新增",
      form: {
        funcName: "",
        funcCode: "",
        remark: "",
        paramsRemark: "",
      },
      rules: {
        // funcName: [
        //   { required: true, message: "函数名称不能为空", trigger: "input" },
        // ],
        // funcCode: [
        //   { required: true, message: "函数标识不能为空", trigger: "input" },
        // ],
      },
    };
  },
  components: {
    Edialog,
  },
  methods: {
    //外部打开弹窗
    async openFn(type, row) {
      console.log(row);
      this.type = type;
      this.outershow = true;
      if (type === "edit") {
        this.title = "修改";
        this.formLoading = true;
        let res = await BsFunctionByIdApi(row.id);
        if (res.data && res.data.bcode == 0) {
          this.form = res.data.bdata;
        }
        this.formLoading = false;
      } else if (type === "detail") {
        this.title = "详情";
        this.formLoading = true;
        let res = await BsFunctionByIdApi(row.id);
        if (res.data && res.data.bcode == 0) {
          this.form = res.data.bdata;
        }
        this.formLoading = false;
      } else {
        this.$nextTick(() => {
          this.$refs.form.resetFields();
        });
        this.title = "新增";
      }
    },
    saveFn() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          if (this.type == "edit") {
            postBsFunctionApi(this.form).then((res) => {
              if (res.data && res.data.bcode == 0) {
                this.$message.success(res.data.bmsg || "修改成功");
                this.outershow = false;
                this.$emit("refreshFn");
              }
            });
          } else {
            // addTemplate(this.form).then((res) => {
            //   if (res.code === 200) {
            //     this.$message.success("新增成功");
            //   }
            // });
          }
        }
      });
    },
    closeFn() {
      this.$refs.form.resetFields();
      this.outershow = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.el-select {
  width: 100%;
}
</style>
